﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Biz_Dal.Biz;

namespace Biz_Dal.Dal
{
    public class DalCTDonDatHang
    {
        //lấy id mới
        internal static int IdMaxCTDDH()
        {
            DalCommon dal = new DalCommon();
            int intIdt = dal.BookIdeas_ShopDataContext.CTDonDatHangs.Max(u => u.idCTDDH);
            return intIdt;
        }
        internal static List<BizCTDonDatHang> GetChiTietDonDatHangByIDDDH(int id)
        {
            DalCommon dal = new DalCommon();
            var result = from p in dal.BookIdeas_ShopDataContext.CTDonDatHangs
                         where p.idDDH == id
                         select new BizCTDonDatHang(p.idCTDDH)
                         {
                             IdCTDDH=p.idCTDDH,
                             DonGia = p.donGia,
                             SoLuongDH = p.soLuongDatHang,
                             IdDDH=p.idDDH,
                             IdSach=p.idSach,
                             DonDatHang = new BizDonDatHang(p.DonDatHang.idDDH),
                             Sach = new BizSach(p.idSach),
                         };
            return result.ToList<BizCTDonDatHang>();
        }

        internal static void Insert(BizCTDonDatHang bizCTDonDatHang)
        {
            DalCommon dal = new DalCommon();
            CTDonDatHang ct = new CTDonDatHang();
            ct.donGia = bizCTDonDatHang.DonGia;
            ct.soLuongDatHang = bizCTDonDatHang.SoLuongDH;
            ct.idSach = bizCTDonDatHang.IdSach;
            ct.idDDH = bizCTDonDatHang.IdDDH;
            ct.thanhTien = bizCTDonDatHang.ThanhTien;

            dal.BookIdeas_ShopDataContext.CTDonDatHangs.InsertOnSubmit(ct);
            dal.BookIdeas_ShopDataContext.SubmitChanges();
        }
    }
}
